package com.syni.mdd.sjb.common.component.utils.common.utils;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.constants.CommonResultCode;
import com.syni.mdd.sjb.common.component.utils.common.constants.CommonResultMsg;
import com.syni.mdd.sjb.common.component.utils.common.enums.ReturnEnums;
import lombok.extern.log4j.Log4j2;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @className RpcResponseVo
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/6 18:33
 * @VERSION 1.0
 **/
@Log4j2
public class RpcResponseVo{
    private String status;
    private Object data;
    private String msg;
    private Map<String,Object> userData = new HashMap<>();

    public RpcResponseVo(){

    }

    public RpcResponseVo(String status, Object data, String msg) {
        this.status = status;
        this.data = data;
        this.msg = msg;
    }

    public RpcResponseVo(String status, Object data, Map<String,Object> userData,String msg) {
        this.status = status;
        this.data = data;
        this.msg = msg;
        this.userData.putAll(userData);
    }

    public static RpcResponseVo success(Object data, Map<String,Object> userData, String msg){
        return new RpcResponseVo(CommonResultCode.CODE_NORMAL,data,userData,msg);
    }

    public static RpcResponseVo success(Object data, String msg){
        return new RpcResponseVo(CommonResultCode.CODE_NORMAL,data,msg);
    }

    public static RpcResponseVo success(Object data){
        return new RpcResponseVo(CommonResultCode.CODE_NORMAL,data, CommonResultMsg.HAND_SUCCESS);
    }

    public static RpcResponseVo result(ReturnEnums returnEnums){
        return new RpcResponseVo(returnEnums.getCode(),null,returnEnums.getMsg());
    }

    public static RpcResponseVo result(String code, String msg){
        return new RpcResponseVo(code,null,msg);
    }



    public <T> T parseObject(Class<T> classes){
        if(this.getStatus().equals(CommonResultCode.CODE_NORMAL)){
            T obj = JSON.parseObject(JSON.toJSONString(this.getData()),classes);
            return obj;
        }
        return null;
    }

    public <T> List parseArray(Class<T> classes){
        if(this.getStatus().equals(CommonResultCode.CODE_NORMAL)){
            List<T> ts = new LinkedList<>();
            List<Map<String,Object>> datas = (List<Map<String,Object>>)this.getData();
            //MapUtil.removeNullValues(datas);
            if(datas.size() > 0) {
                datas.forEach(da -> {
                    String text = JSON.toJSONString(da);
                    System.out.println(text);
                    T obj = JSON.parseObject(text, classes);
                    ts.add(obj);
                });
            }
            return ts;
        }
        return null;
    }

    public boolean isSuccess(){
        return CommonResultCode.CODE_NORMAL.equals(this.getStatus());
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Object getDataElement(String dataKey) {
        Map<String,Object> dataMap = (Map<String, Object>) data;
        if(dataMap.containsKey(dataKey)){
            return dataMap.get(dataKey);
        }
        return null;
    }


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getUserData() {
        return userData;
    }

    public Object getUserDataElement(String key){
        if(userData.containsKey(key)){
            return userData.get(key);
        }
        return null;
    }

    public void setUserData(Map<String, Object> userData) {
        this.userData = userData;
    }

    public Integer getUserDataIntElement(String key) {
        Object value = getUserDataElement(key);
        if(value != null){
            return Integer.parseInt(value.toString());
        }
        return 0;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public void info() {
        log.info("rpcVo:{}",this.toString());
    }
}
